package com.easipay.designpattern.observer.subject;

import java.util.ArrayList;
import java.util.List;

import com.easipay.designpattern.observer.observer.Observer;

/**
 * concreate interface
 * @author Administrator
 *
 */
public class WeatherData implements Subject {
 
    private List<Observer> observers;
    
    private float temperature;
    
    private float humidity;
    
    private float pressure;
    
    public WeatherData(){
    	this.observers = new ArrayList<Observer>();
    }
	
	public void notifyObservers() {
		for (Observer observer : observers) {
			observer.update(temperature, humidity, pressure);
		}

	}

	public void registerObserver(Observer o) {
		observers.add(o);
	}

	public void removeObserver(Observer o) {
		int index = observers.indexOf(o);
		if(index>=0){
			observers.remove(index);
		}
	}
	
	
	public float getTemperature() {
		return temperature;
	}

	public void setTemperature(float temperature) {
		this.temperature = temperature;
	}

	public float getHumidity() {
		return humidity;
	}

	public void setHumidity(float humidity) {
		this.humidity = humidity;
	}

	public float getPressure() {
		return pressure;
	}

	public void setPressure(float pressure) {
		this.pressure = pressure;
	}

	//测量发生变化通知观察者
	public void meansurementsChanged(){
		notifyObservers();
	}
	
	public void setMeansurements(float temperature, float humidity, float pressure){
		this.temperature = temperature;
		this.humidity = humidity;
		this.pressure = pressure;
		meansurementsChanged();
	}
	
}
